package goods

import (
	"encoding/json"
	// "fmt"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	"net/http"
	"server/conf"
	"server/framework/API/R"
	"server/lib/Imartini"
	"server/lib/session"
	"server/lib/upload"
	"server/web/model/shop/admin/goods"
	"strconv"
	"time"
)

var (
	wlogo, _ = strconv.Atoi(conf.GET["app"]["logowidth"])
	hlogo, _ = strconv.Atoi(conf.GET["app"]["logoheight"])
)

func class(r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	class := goods.Class(info["id"].(string))

	r.HTML("shop/admin/goods/class.html", map[string]interface{}{
		"class": class,
	})
}

func delclass(params martini.Params, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) string {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)
	id := params["id"]

	//-1：不是末级科目，0：类别下还有商品，1：删除成功
	return goods.DelClass(id, info["id"].(string))
}

func addclass(res http.ResponseWriter, req *http.Request, c martini.Context, mlog *Imartini.Mlog, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	path, err := upload.ImgSave(req, "imgurl", wlogo, hlogo, info["id"].(string))
	if err != nil && err.Error() == "-1" {
		res.Write([]byte("上传的图片类型不正确"))
		return
	}
	classname := req.Form["classname"][0]
	upperid := req.Form["upperid"][0]
	remark := req.Form["remark"][0]
	imgurl := path

	param := map[string]interface{}{
		"uid":         info["id"],
		"name":        classname,
		"upperid":     upperid,
		"imgurl":      imgurl,
		"remarks":     remark,
		"create_time": int(time.Now().Unix()),
	}
	goods.AddClass(param)
	http.Redirect(res, req, req.URL.Path, http.StatusFound)
}

func updateclass(params martini.Params, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	path, err := upload.ImgSave(req, "imgurl", wlogo, hlogo, info["id"].(string))
	if err != nil && err.Error() == "-1" {
		res.Write([]byte("上传的图片类型不正确"))
	} else if err != nil {
		mlog.Printf("%v:%s", err, path)
	}
	p := make(map[string]interface{})
	if path != "" {
		p["imgurl"] = path
	}
	p["name"] = req.FormValue("classname")
	p["remarks"] = req.FormValue("remark")
	_, err = goods.UpdateClass(info["id"].(string), params["id"], p)
	if err != nil {
		mlog.Printf("%v", err)
	}
	http.Redirect(res, req, "/admin/goods/class", http.StatusFound)
}

func getclassinfo(params martini.Params, r render.Render, res http.ResponseWriter, req *http.Request, g Imartini.G, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	class := goods.Classinfo(info["id"].(string), params["id"])

	if len(class) > 0 {
		R.Write(res, g, map[string]interface{}{"class": class[1]})
	} else {
		R.Write(res, g, map[string]interface{}{})
	}
}
